package com.elinkway.infinitemovies.http.parser;

import com.elinkway.infinitemovies.bean.Episode;
import com.elinkway.infinitemovies.bean.PageInfoData;
import com.elinkway.infinitemovies.ui.activity.play.PlayerUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class PageInfoParser extends LVideoMobileParser<PageInfoData> {

    private String mSite ="";
    
    private String mSetDataOrder = "";
    
    private ArrayList<Episode> mEpisodes;
    
    private JSONArray pageInfoArr ;
    
    public PageInfoParser(ArrayList<Episode> episodes, String site, String setDataOrder) {
        mEpisodes = episodes;
        mSite = site;
        mSetDataOrder = setDataOrder;
    }

    @Override
    public PageInfoData parse(JSONObject data) throws Exception {
        int pageInfoSize = pageInfoArr.length();
        if (null != data && pageInfoSize > 0) {
            for (int i = 0; i < pageInfoSize; i++) {
                JSONObject pageInfoItem = pageInfoArr.optJSONObject(i);
                if (mSite.equals(pageInfoItem.optString("site"))) {
                    JSONArray videoListArr = pageInfoItem.optJSONArray("videoList");
                    if (null != videoListArr && videoListArr.length() > 0) {
                        PageInfoData pageInfoData = new PageInfoData();
                        int videoListSize = videoListArr.length();
                        if (null == mEpisodes) {
                            mEpisodes = new ArrayList<Episode>(videoListSize);
                        }

                        if (PlayerUtils.FIRST.equals(mSetDataOrder)) {
                            for (int j = videoListSize - 1; j >= 0; j--) {
                                JSONObject episodeObj = videoListArr.optJSONObject(j);
                                Episode episode = getEpisodeData(pageInfoItem, episodeObj);
                                // 倒序像第一个位置插入数据
                                mEpisodes.add(0, episode);
                            }
                        } else if (PlayerUtils.END.equals(mSetDataOrder)) {
                            for (int j = 0; j < videoListSize; j++) {
                                JSONObject episodeObj = videoListArr.optJSONObject(j);
                                Episode episode = getEpisodeData(pageInfoItem, episodeObj);
                                mEpisodes.add(episode);
                            }
                        }
                        pageInfoData.setmEpisodes(mEpisodes);
                        return pageInfoData;
                    }
                }
            }
        }
        return null;
    }
    
    private Episode getEpisodeData(JSONObject pageInfoItem, JSONObject episodeObj) {
        Episode episode = new Episode();
        episode.setSrc(pageInfoItem.optString("src"));
        episode.setName(episodeObj.optString("name"));
        episode.setPlay_url(episodeObj.optString("url"));
        episode.setPorder(episodeObj.optString("porder"));
        episode.setPls(episodeObj.optString("pls"));
        episode.setMid(episodeObj.optString("mid"));
        episode.setGlobaVid(episodeObj.optString("globalVid"));
        episode.setIsdownload(episodeObj.optString("isdownload"));
        episode.setDataType(Integer.parseInt(episodeObj.optString("dataType")));
        episode.setCloudId(episodeObj.optString("cloudId"));
        episode.setVid(episodeObj.optString("vid"));
        if (pageInfoItem.has("subname")) {
            try {
                String subname = episodeObj.getString("subname");
                episode.setSubName(subname);
            } catch (JSONException e) {
                episode.setSubName("");
            }
        }
        return episode;
    }
    
    @Override
    protected JSONObject getData(String data) throws JSONException {
        pageInfoArr = new JSONArray(data);
        data = "{\"data\":\"data\"}";
        return super.getData(data);
    }
    
    

}
